// pages/orderList/orderList.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoading: 1,//是否显示加载中图标
    is_self:1,//是否为自己的订单列表
    keywords: '',//搜索关键词
    serarching: 0,//判断是否正在搜索
    orderList: [],//存储订单列表
    page: 1,//订单列表页码
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    navigationList: []//导航列表
  },
  // 开始搜索
  starSearch: function (e) {
    var _this = this;
    console.log(e.detail.value);

    if (_this.data.currentTab == 0) {
      _this.setData({
        orderList: [],
        page: 1,
        keywords: e.detail.value,
        serarching: 0
      });
      _this.getOrderList(e.detail.value);
    } else {
      _this.setData({
        currentTab: 0,
        keywords: e.detail.value,
        serarching: 0
      });
    }
  },
  // 点击带图片的搜索框出现可输入的input框
  search: function () {
    var _this = this;
    _this.setData({
      serarching: 1
    });
  },
  // 前往分享
  goShare: function (e) {
    var goodsId = e.currentTarget.dataset.goods_id;
    var formId = e.detail.formId;
    app.postFormId(this.data.token, formId);
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + goodsId,
    })
  },
  // 查看订单详情
  viewOrderDetail: function (e) {
    var orderId = e.currentTarget.dataset.order_id;
    var uid = e.currentTarget.dataset.uid;
    var is_self = this.data.is_self;
    var formId = e.detail.formId;
    app.postFormId(this.data.token, formId);
    if (is_self==1){
      wx.navigateTo({
        url: '/pages/orderDetail/orderDetail?orderId=' + orderId,
      })
    }else{
      wx.navigateTo({
        url: '/pages/orderDetail/orderDetail?orderId=' + orderId+'&uid='+uid,
      })
    }
    
  },
  // 获取更多订单列表
  getMoreOrderList: function () {
    var _this = this;
    _this.getOrderList(_this.data.keywords);
  },
  // 获取订单列表
  getOrderList: function (keywords) {
    var _this = this;
    _this.setData({
      isLoading:1
    })
    var navigationList = _this.data.navigationList;
    var currentTab = _this.data.currentTab;
    currentTab = navigationList[currentTab].id;
      var requestData = {
        token: _this.data.token,
        version: app.setConfig.version,
        type: app.setConfig.type,
        keywords: keywords,
        order_type:currentTab,
        is_self: _this.data.is_self,
        is_bargain: _this.data.is_bargain,
        page: _this.data.page
      }

    app.post('/Order/orderList', requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        var orderList = _this.data.orderList;
        orderList = orderList.concat(res.data.list);
        _this.setData({
          isLoading:0,
          page: res.data.page,
          orderList: orderList
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    var _this = this;
    var keywords = _this.data.keywords;
    var current = e.detail.current;
    if (keywords != '') {
      _this.setData({
        page: 1,
        orderList: [],
        currentTab: e.detail.current
      });
    } else {
      _this.setData({
        keywords: '',
        page: 1,
        orderList: [],
        currentTab: e.detail.current
      });
    }
    _this.checkCor();
    _this.getOrderList(_this.data.keywords);
   
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var _this = this;
    var cur = e.target.dataset.current;
    if (_this.data.currentTab == cur) {
      _this.setData({
        page: 1,
        keywords: '',
        orderList: []
      });
      _this.getOrderList(_this.data.keywords);
      return false;
    } else {
      _this.setData({
        keywords: '',
        page: 1,
        orderList: [],
        currentTab: cur
      });
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  footerTap: app.footerTap,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var configs = wx.getStorageSync('configs');
    if (options.is_self == 1) {
      _this.setData({
        navigationList: [{ title: '全部', id: 0, index: 0 }, { title: '待支付', id: 2, index: 1 }, { title: '待砍价', id: 6, index: 2 }, { title: '待发货', id: 3, index: 3 }, { title: '待收货', id: 4, index: 4 }, { title: '已完成', id: 5, index: 5 }]
      })
    } else {
      _this.setData({
        navigationList: [{ title: '全部', id: 0, index: 0 }, { title: '待支付', id: 2, index: 1 }, { title: '待发货', id: 3, index: 2 }, { title: '待收货', id: 4, index: 3 }, { title: '已完成', id: 5, index: 4 }]
      })
    }
    if (options.current) {
      _this.setData({
        orderList: [],//存储订单列表
        page: 1,//订单列表页码
        currentTab: options.current,
        is_self: options.is_self,
        is_bargain: options.is_bargain
      })
      _this.loop(1);
    } else {
      _this.loop(0);
    }
   

    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        _this.setData({
          configs: configs,
          winHeight: calc
        });
      }
    });
  },
  // 判断是否获取到token的值了
  loop: function (isImplement) {
    var _this = this;
    // console.log(isImplement);
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop(isImplement) }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      var currentTab = _this.data.currentTab;
        _this.getOrderList("");
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    console.log(_this.data.keywords);
    _this.getOrderList(_this.data.keywords);
  }
})